auto.waitFor()

var appName = "多点";
launchApp(appName);
sleep(100);
console.show();
var diff_time = getDuoDianTime();
console.log('延迟=' + diff_time);
var seck_time = getNextSeckillTime();
console.log(new Date(seck_time));
run();

function run() {
    
    // 时间判断是否可以开始抢购（调试时输出倒计时）
    while(true) {
        var curr_datetime = new Date().getTime();
        if (curr_datetime + diff_time >= seck_time) {
            break;
        }
    }

    doSeckill();
}


// 执行抢购逻辑
function doSeckill() {
    // 启动线程
    threads.start(function () {
        // 时间相关
        loop1();
    });
}


// 循环1查找立即抢购按钮
function loop1() {
    console.log('执行立即抢购');
    while (true) {
        var do1 = className("android.widget.Button").text('立即抢购').findOnce();
        if (do1) {
            console.log('点击【立即抢购】');
            do1.click();
            threads.start(function () {
                loop2();
            });
            break;
        }
    }
}


// 循环2查找预约抢购按钮，点击并结束循环1并启动循环3；若是 今日已售罄，每天10:00开始抢购 则返回前一页
function loop2() {
    console.log('进入预约抢购页面');
    while (true) {
        var do3_press = className("android.widget.Button").text('预约抢购').findOnce();
        if (do3_press) {
            console.log('点击【预约抢购】');
            do3_press.click();
            threads.start(function () {
                loop3();
            });
            break;
        }

        var do2_back = className("android.widget.Button").text('今日已售罄，每天10:00开始抢购').findOnce();
        if (do2_back) {
            console.log('预约还未开始【返回】');
            back();
            threads.start(function () {
                loop1();
            });
            break;
        }
    }
}


// 循环3查找20秒后的提交订单按钮 （根据弹窗情况决定是重复提交还是结束抢购）
function loop3() {
    console.log('进入确认下单页面');
    while (true) {
        var sub_order = className("android.widget.Button").text('确认下单').findOnce();
        if (sub_order) {
            console.log('点击【确认下单】');
            sub_order.click();
        }

        var ok_btn = className("android.widget.Button").text('确认').findOnce();
        if (ok_btn) {
            ok_btn.click();
        }

        sleep(200);
    }
}

// 物美多点时间戳和当前时间戳的差
function getDuoDianTime() {
    var response_duodian = http.get("https://appapis.dmall.com/static/queryUserCheckInfo.jsonp");
    var duodian_Time = response_duodian.body.string();
    var json = eval(duodian_Time)
    var duodian_Time = json.result.data.currentTime;
    var duodian_number_time = Number(duodian_Time);
    // toastLog("多点时间戳为:" + duodian_number_time);
    var cur = new Date().getTime();
    // toastLog("当前时间戳为:" + cur);
    // toastLog("时间差：" + (duodian_number_time - cur))
    // console.log(new Date(duodian_number_time));
    return duodian_number_time - cur;
}

// 获取下一个可抢购的10点的时间戳
function getNextSeckillTime() {
    var now = new Date();
    var curr_year = now.getFullYear();
    var curr_month = now.getMonth() + 1;
    var curr_date = now.getDate();

    var seckill_time_str = curr_year + '/' + curr_month + '/' + curr_date + ' 10:00:00';
    // console.log(seckill_time_str);
    seckill_date = new Date(seckill_time_str);
    // console.log(seckill_date);
    // console.log(seckill_date.getTime());

    if (now.getTime() > seckill_date.getTime()) {
        seckill_date = new Date(seckill_date.getTime() + 24 * 60 * 60 * 1000);
    }

    return seckill_date.getTime();
}
